<?php
/***************************************************************************
 *                               install.php
 *                            -------------------
 *                       (APU) Automated Pilot Utility
 *   begin                : Sunday, July 24, 2005
 *   copyright            : (C) 2004-2006 SF Games
 *   email                : techsupport@sf-games.com
 *
 *   Expects the following POST variable:
 *
 *           Database Host: dbhost
 *           Fatabase Name: dbname
 *           Database User: dbuser
 *       Database Password: dbpass
 ***************************************************************************/

//	$apus_root_path = './';
//	include($apus_root_path . 'common.inc');
//	include($apus_root_path . 'config.inc');

	$dbHost = "";
	$dbName = "";
	$dbUser = "";
	$dbPass = "";
	$bRedo  = false;

	//
	// Validate the POST variables
	//

	if ( isset($_POST['dbhost']) )
	{
		if ( strlen($_POST['dbhost']) > 0 )
		{
			$dbHost = trim($_POST['dbhost']);
		}
		else
		{
			$sErrorsArray[] = "The given database host name is empty<br>";
			$bRedo = true;
		}
	}

	if ( isset($_POST['dbname']) )
	{
		if ( strlen($_POST['dbname']) > 0 )
		{
			$dbName = trim($_POST['dbname']);
		}
		else
		{
			$sErrorsArray[] = "The given database name is empty<br>";
			$bRedo = true;
		}
	}

	if ( isset($_POST['dbuser']) )
	{
		if ( strlen($_POST['dbuser']) > 0 )
		{
			$dbUser = trim($_POST['dbuser']);
		}
		else
		{
			$sErrorsArray[] = "The given database user name is empty<br>";
			$bRedo = true;
		}
	}

	if ( isset($_POST['dbpass']) )
	{
		if ( strlen($_POST['dbpass']) > 0 )
		{
			$dbPass = trim($_POST['dbpass']);
		}
		else
		{
			$sErrorsArray[] = "The given database password is empty<br>";
			$bRedo = true;
		}
	}

	//
	// On bad entry, give link back to the PIREP submission form
	//

	if ( $bRedo == true )
	{
		//
		// Show the errors
		//

		echo("<p align=\"center\"><font color=\"#990000\" size=\"4\" face=\"Tahoma\"><b>There are errors in your install form submission! Press your browsers back button to try again</b></font></p>");

		echo("<div align=\"center\">");
          echo("<table width=\"480\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">");
            echo("<tr>");
			  echo("<ul>");
			    echo("<td width=\"491\" height=\"227\">");

		foreach ($sErrorsArray as $i => $value)
		{
			  echo("<li>");
			  echo($sErrorsArray[$i]);
			  echo("</li>");
		}

			    echo("</td>");
              echo("</ul>");
            echo("</tr>");
          echo("</table>");
        echo("</div>");
	}
	else
	{
		//
		// Attempt to connect to the database
		//

		$db = mysql_connect($dbHost, $dbUser, $dbPass)
			or die("Could not connect to the SQL server<br>");
		echo("Connected to database!<br>");
		mysql_select_db($dbName);
		echo("Database Selected!<br>");

		//
		// Create the pilots table
		//

		$query = "CREATE TABLE IF NOT EXISTS " .$table_prefix ."PILOTS (
				  'id' int(11) unsigned NOT NULL auto_increment,
                  `PILOT_ID` char(15) NOT NULL,
                  `PASSWORD` char(40) default NULL,
                  `FIRST_NAME` char(30) default NULL,
                  `LAST_NAME` char(40) default NULL,
                  `EMAIL` char(50) default NULL,
                  `RANK_AC` tinyint(3) unsigned default '1',
                  `RANK_HELO` tinyint(3) unsigned default '1',
                  `HOURS_AC` float default '0',
                  `HOURS_HELO` float default '0',
                  `TOTAL_HRS` float default '0',
                  `CUR_BID` int(10) unsigned default '0',
                  `BID` char(100) default NULL,
                  `DART` tinyint(3) unsigned default '0',
                  `SAR` tinyint(3) unsigned default '0',
                  `FLEET_SUP` tinyint(3) unsigned default '0',
                  `LOA` tinyint(3) unsigned default '0',
                  `ACTIVE` tinyint(3) unsigned default '1',
                  `JOINED` date default NULL,
                  `LAST_FLY` date default NULL,
                  'DELETED' tinyint(3) unsigned DEFAULT '0',
                  PRIMARY KEY  (`id`)
                ) TYPE=MyISAM COMMENT='APU Pilots table';";

		$result = mysql_query($query);
		if ( !$result )
		{
			echo(mysql_error());
			echo("<br>");
			die("Creation of table PILOTS failed.<br>");
		}
		echo("Created table PILOTS!<br>");

		//
		// Create PIREP table
		//

        $query = "CREATE TABLE IF NOT EXISTS " .$table_prefix ."PIREP (
          'id' int(11) unsigned NOT NULL auto_increment,
          `PILOT_ID` char(15) NOT NULL default '',
          `FLIGHT_DATE` date default NULL,
          `AC_TYPE` char(20) default NULL,
          `TAIL_NUM` char(15) default NULL,
          `FLIGHT_NUM` char(15) default NULL,
          `DEPART` char(4) default NULL,
          `DEST` char(4) default NULL,
          `TIME_OUT` int(11) default '0',
          `TIME_IN` int(11) default '0',
          `TIME_FIXED` float default '0',
          `TIME_HELO` float default '0',
          `FUEL_OUT` int(11) default '0',
          `FUEL_IN` int(11) default '0',
          `FUEL_BURN` int(11) default '0',
          `PAX` int(11) default '0',
          `CARGO` int(11) default '0',
          `COMMENTS` char(255) default NULL,
          PRIMARY KEY (id),
   		  KEY FLIGHT_DATE (FLIGHT_DATE)
        ) TYPE=MyISAM COMMENT='APU Pirep table';";

		$result = mysql_query($query);
		if ( !$result )
		{
			echo(mysql_error());
			echo("<br>");
			die("Creation of table PIREP failed.<br>");
		}
		echo("Created table PIREP!<br>");

		//
		// Create and populate A/C rank table
		//

        $query = "CREATE TABLE IF NOT EXISTS " .$table_prefix ."FIXEDWING_AC_RANKS (
                  RANK_ID INT UNSIGNED DEFAULT NULL,
                  RANK_STR CHAR(50) DEFAULT NULL,
                  RANK_PIC CHAR(255) DEFAULT NULL,
                  MIN_HRS FLOAT DEFAULT NULL,
                  MAX_HRS FLOAT DEFAULT NULL
                  )TYPE=MyISAM COMMENT='Fixed Wing Rank Table';";

		$result = mysql_query($query);
		if ( !$result )
		{
			echo(mysql_error());
			echo("<br>");
			die("Creation of table FIXEDWING_AC_RANKS failed.<br>");
		}
		echo("Created table FIXEDWING_AC_RANKS!<br>");

		// Insert values into the table
		$query = "INSERT INTO " .$table_prefix ."FIXEDWING_AC_RANKS VALUES ( 1, 'First Officer', 'img/fo.gif', 0.0, 9.99 );";
		$result = mysql_query($query);
		if ( !$result )
			die("Adding AC rank values failed.<br>Failed with mySQL error: " . mysql_error());
		$query = "INSERT INTO " .$table_prefix ."FIXEDWING_AC_RANKS VALUES ( 2, 'Captain', 'img/capt.gif', 10.0, 39.99 );";
		$result = mysql_query($query);
		if ( !$result )
			die("Adding AC rank values failed.<br>Failed with mySQL error: " . mysql_error());
		$query = "INSERT INTO " .$table_prefix ."FIXEDWING_AC_RANKS VALUES ( 3, 'Senior Captain', 'img/snrcapt.gif', 40.0, 99.99 );";
		$result = mysql_query($query);
		if ( !$result )
			die("Adding AC rank values failed.<br>Failed with mySQL error: " . mysql_error());
		$query = "INSERT INTO " .$table_prefix ."FIXEDWING_AC_RANKS VALUES ( 4, 'Commercial Captain', 'img/comcapt.gif', 100.0, 249.99 );";
		$result = mysql_query($query);
		if ( !$result )
			die("Adding AC rank values failed.<br>Failed with mySQL error: " . mysql_error());
		$query = "INSERT INTO " .$table_prefix ."FIXEDWING_AC_RANKS VALUES ( 5, 'Senior Commercial Captain', 'img/sencomcapt.gif', 250.0, 499.99 );";
		$result = mysql_query($query);
		if ( !$result )
			die("Adding AC rank values failed.<br>Failed with mySQL error: " . mysql_error());
		$query = "INSERT INTO " .$table_prefix ."FIXEDWING_AC_RANKS VALUES ( 6, 'ATP Captain', 'img/atpcapt.gif', 500.0, 749.99 );";
		$result = mysql_query($query);
		if ( !$result )
			die("Adding AC rank values failed.<br>Failed with mySQL error: " . mysql_error());
		$query = "INSERT INTO " .$table_prefix ."FIXEDWING_AC_RANKS VALUES ( 7, 'Senior ATP Captain', 'img/senaptcapt.gif', 750.0, 999.99 );";
		$result = mysql_query($query);
		if ( !$result )
			die("Adding AC rank values failed.<br>Failed with mySQL error: " . mysql_error());
		$query = "INSERT INTO " .$table_prefix ."FIXEDWING_AC_RANKS VALUES ( 8, 'ATP Master', 'img/atpmast.gif', 1000.0, 3499.99 );";
		$result = mysql_query($query);
		if ( !$result )
			die("Adding AC rank values failed.<br>Failed with mySQL error: " . mysql_error());
		$query = "INSERT INTO " .$table_prefix ."FIXEDWING_AC_RANKS VALUES ( 9, 'Check Airman', 'img/chkairman.gif', 3500.00, 99999999.99 );";
		$result = mysql_query($query);
		if ( !$result )
			die("Adding AC rank values failed.<br>Failed with mySQL error: " . mysql_error());

		//
		// Create and populate Helicopter ranks table
		//

        $query = "CREATE TABLE " .$table_prefix ."HELO_AC_RANKS
                 (
                 RANK_ID INT UNSIGNED DEFAULT NULL,
                 RANK_STR CHAR(50) DEFAULT NULL,
                 RANK_PIC CHAR(255) DEFAULT NULL,
                 MIN_HRS FLOAT DEFAULT NULL,
                 MAX_HRS FLOAT DEFAULT NULL
                 )TYPE=MyISAM COMMENT='Helicopter Rank Table';";

		$result = mysql_query($query);
		if ( !$result )
		{
			echo(mysql_error());
			echo("<br>");
			die("Creation of table HELO_AC_RANKS failed.<br>");
		}
		echo("Created table HELO_AC_RANKS!<br>");

        $query = "INSERT INTO " .$table_prefix ."HELO_AC_RANKS VALUES ( 1, 'Student', 'img/h_fo.gif', 0.0, 9.99 );";
		$result = mysql_query($query);
		if ( !$result )
			die("Adding Helo rank values failed.<br>Failed with mySQL error: " . mysql_error());
        $query = "INSERT INTO " .$table_prefix ."HELO_AC_RANKS VALUES ( 2, 'Pilot', 'img/h_capt.gif', 10.0, 39.99 );";
		$result = mysql_query($query);
		if ( !$result )
			die("Adding Helo rank values failed.<br>Failed with mySQL error: " . mysql_error());
        $query = "INSERT INTO " .$table_prefix ."HELO_AC_RANKS VALUES ( 3, 'Instrument Pilot', 'img/h_srcapt.gif', 20.0, 49.99 );";
		$result = mysql_query($query);
		if ( !$result )
			die("Adding Helo rank values failed.<br>Failed with mySQL error: " . mysql_error());
        $query = "INSERT INTO " .$table_prefix ."HELO_AC_RANKS VALUES ( 4, 'Commercial Pilot', 'img/h_comcapt.gif', 50.0, 99.99 );";
		$result = mysql_query($query);
		if ( !$result )
			die("Adding Helo rank values failed.<br>Failed with mySQL error: " . mysql_error());
        $query = "INSERT INTO " .$table_prefix ."HELO_AC_RANKS VALUES ( 5, 'Senior Commercial Pilot', 'img/h_srcomcapt.gif', 100.0, 249.99 );";
		$result = mysql_query($query);
		if ( !$result )
			die("Adding Helo rank values failed.<br>Failed with mySQL error: " . mysql_error());
        $query = "INSERT INTO " .$table_prefix ."HELO_AC_RANKS VALUES ( 6, 'Advanced Commercial Pilot', 'img/h_atp.gif', 250.0, 499.99 );";
		$result = mysql_query($query);
		if ( !$result )
			die("Adding Helo rank values failed.<br>Failed with mySQL error: " . mysql_error());
        $query = "INSERT INTO " .$table_prefix ."HELO_AC_RANKS VALUES ( 7, 'Instructor', 'img/h_sratp.gif', 500.0, 749.99 );";
		$result = mysql_query($query);
		if ( !$result )
			die("Adding Helo rank values failed.<br>Failed with mySQL error: " . mysql_error());
        $query = "INSERT INTO " .$table_prefix ."HELO_AC_RANKS VALUES ( 8, 'Chief Instructor', 'img/h_atpmaster.gif', 750.0, 999.99 );";
		$result = mysql_query($query);
		if ( !$result )
			die("Adding Helo rank values failed.<br>Failed with mySQL error: " . mysql_error());
        $query = "INSERT INTO " .$table_prefix ."HELO_AC_RANKS VALUES ( 9, 'Check Airman', 'img/h_chkair.gif', 1000.00, 99999999.99 );";
		$result = mysql_query($query);
		if ( !$result )
			die("Adding Helo rank values failed.<br>Failed with mySQL error: " . mysql_error());

		echo("<br>Finished creation of APU tables.<br>");
	}
?>
